//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package objects;

import objects.OrderEntry.state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class Orders {
    private final List<OrderEntry> orderList = new ArrayList();
    private OrderEntry shipped = null;

    public Orders() {
    }

    public void addOrder(OrderEntry oe) {
        this.orderList.add(oe);
    }

    public void addEmptyOrder(Integer lvl) {
        OrderEntry oe = new OrderEntry(lvl, (Integer)null, Integer.valueOf(this.orderList.size()));
        this.orderList.add(oe);
    }

    public void clear() {
        this.orderList.clear();
        this.shipped = null;
    }

    public Iterator<OrderEntry> iterator() {
        return this.orderList.iterator();
    }

    public int size() {
        return this.orderList.size();
    }

    public int getExpReward() {
        int exp = 0;
        Iterator var3 = this.orderList.iterator();

        while(var3.hasNext()) {
            OrderEntry oe = (OrderEntry)var3.next();
            if(!oe.isEmpty()) {
                exp += oe.exp().intValue();
            }
        }

        return exp;
    }

    public boolean isAllReady(LinkedHashMap<Integer, Integer> inventory) {
        Iterator var3 = this.orderList.iterator();

        OrderEntry oe;
        do {
            if(!var3.hasNext()) {
                return true;
            }

            oe = (OrderEntry)var3.next();
        } while(oe.isEmpty() || oe.isReadyToShip(inventory));

        return false;
    }

    public int getReadyExpReward(LinkedHashMap<Integer, Integer> inventory) {
        int exp = 0;
        Iterator var4 = this.orderList.iterator();

        while(var4.hasNext()) {
            OrderEntry oe = (OrderEntry)var4.next();
            if(!oe.isEmpty() && oe.isReadyToShip(inventory)) {
                exp += oe.exp().intValue();
            }
        }

        return exp;
    }

    public int getMinExpIndex() {
        int idx = -1;
        int lastExp = 1000000;
        Iterator var4 = this.orderList.iterator();

        while(var4.hasNext()) {
            OrderEntry oe = (OrderEntry)var4.next();
            if(!oe.isEmpty() && oe.exp().intValue() < lastExp) {
                idx = oe.index().intValue();
                lastExp = oe.exp().intValue();
            }
        }

        return idx;
    }

    public OrderEntry getAcceptedOrder(LinkedHashMap<Integer, Integer> inventory) {
        if(this.shipped != null) {
            return this.shipped;
        } else {
            Iterator i = this.orderList.iterator();

            while(i.hasNext()) {
                OrderEntry oe = (OrderEntry)i.next();
                if(!oe.isEmpty()) {
                    if(oe.isReadyToShip(inventory)) {
                        oe.setState(state.readyToShip);
                        return oe;
                    }

                    oe.setState(state.needToCancel);
                    return oe;
                }
            }

            return null;
        }
    }

    public OrderEntry getFirstReadyToShip(LinkedHashMap<Integer, Integer> inventory) {
        Iterator i = this.orderList.iterator();

        OrderEntry oe;
        do {
            if(!i.hasNext()) {
                return null;
            }

            oe = (OrderEntry)i.next();
        } while(oe.isEmpty() || !oe.isReadyToShip(inventory));

        return oe;
    }

    public void sort() {
        Collections.sort(this.orderList);
    }

    public void addToShipped(OrderEntry oe) {
        this.orderList.remove(oe);
        this.shipped = oe;
        this.shipped.setState(state.Shipped);
    }

    public void clearShipped() {
        this.shipped = null;
    }
}
